package lovo01.homework03;

import java.util.Scanner;

/**
 * 学生管理的主界面类
 */
public class StudentUI {
    public Scanner sc = new Scanner(System.in);
    public SuperArray superArray = new SuperArray();

    /**
     * 主流程方法
     */
    public void process(){
        System.out.println("欢迎来到学生管理系统");
            menu();
    }
    /**
     * 主菜单
     */
    public void menu(){
        while (true){
            System.out.println("请选择: 1-增加学生 2-删除学生 3-查询学生列表 4-修改学生信息 5-退出系统");
            int num = sc.nextInt();
            if (num == 1) {
                add();
            }
            if (num == 2){
                remove();
            }
            if (num == 3){
                search();
            }
            if (num == 4){
                amend();
            }
            if (num == 5){
                System.out.println("退出系统");
                System.exit(0);
            }else if(num < 0 || num > 5){
                System.out.println("输入错误，请重新输入");
            }
        }
    }

    /**
     * 增加
     */
    public void add(){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入学生性别：");
        String gender = sc.next();
        Student student = new Student(name,age,gender);
        superArray.add(student);
    }

    /**
     * 删除
     */
    public void remove(){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        for (int i = 0;i < superArray.size();i++){
            Student student = (Student) superArray.get(i);
            if (student.getName().equals(name)){
                superArray.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("没有该学生");
    }

    /**
     * 查询
     */
    public void search(){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        for (int i = 0;i < superArray.size();i++){
            Student student = (Student) superArray.get(i);
            if (student.getName().equals(name)){
                System.out.println("该学生姓名为：" + student.getName() + "\n"
                        + "年龄为：" + student.getAge() + "\n" + "性别为："
                        + student.getGender());
                return;
            }
        }
        System.out.println("没有该学生");
    }

    /**
     * 修改
     */
    public void amend(){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        for (int i = 0;i < superArray.size();i++) {
            Student student = (Student) superArray.get(i);
            if (student.getName().equals(name)){
                System.out.println("请输入学生姓名：");
                String name01 = sc.next();
                System.out.println("请输入学生年龄：");
                int age01 = sc.nextInt();
                System.out.println("请输入学生性别：");
                String gender01 = sc.next();
                student.setName(name01);
                student.setAge(age01);
                student.setGender(gender01);
                return;
            }
        }
        System.out.println("没有该学生");
    }
}
